package com.example.mystoryapp2.data.network.model

import android.util.Log
import com.example.mystoryapp2.data.network.NetCallBack
import com.example.mystoryapp2.data.network.ServiceCreator
import com.example.mystoryapp2.data.network.service.PageService
import com.example.mystoryapp2.domain.Page
import com.example.mystoryapp2.domain.RecordStatic
import com.example.mystoryapp2.domain.Records
import com.example.mystoryapp2.domain.Story
import retrofit2.Call
import retrofit2.Response

object PageModel {
    private val TAG = "StoryModel"
    private var pageService = ServiceCreator.create<PageService>()

    init {
        Log.d(TAG, ": StoryModel执行了")
    }


    fun add(page: Page, callBack: ServiceCreator.ICallBack<Int>) {
        pageService.add(page)
            .enqueue(object : NetCallBack<Int>() {
                override fun onResponse(call: Call<Int>, response: Response<Int>) {
                    val body = response.body()
                    if (body != null) {
                        callBack.onSuccess(body)
                    } else {
                        callBack.onSuccess(-1)
                    }
                }

                override fun onFailure(call: Call<Int>, t: Throwable) {
                    callBack.onSuccess(-1)
                }
            })
    }


    fun delete(id: Int, callBack: ServiceCreator.ICallBack<Boolean>) {
        pageService.delete(id)
            .enqueue(object : NetCallBack<Boolean>() {
                override fun onResponse(call: Call<Boolean>, response: Response<Boolean>) {
                    val body = response.body()
                    if (body != null) {
                        callBack.onSuccess(body)
                    } else {
                        callBack.onSuccess(false)
                    }
                }

                override fun onFailure(call: Call<Boolean>, t: Throwable) {
                    callBack.onSuccess(false)
                }
            })
    }

    fun update(page: Page, callBack: ServiceCreator.ICallBack<Boolean>) {
        pageService.update(page)
            .enqueue(object : NetCallBack<Boolean>() {
                override fun onResponse(call: Call<Boolean>, response: Response<Boolean>) {
                    val body = response.body()
                    if (body != null) {
                        callBack.onSuccess(body)
                    } else {
                        callBack.onSuccess(false)
                    }
                }

                override fun onFailure(call: Call<Boolean>, t: Throwable) {
                    callBack.onSuccess(false)
                }
            })
    }

    fun getListByOId(id: Int, callBack: ServiceCreator.ICallBack<List<Page>>) {
        pageService.getListByOId(id)
            .enqueue(object : NetCallBack<List<Page>>() {
                override fun onResponse(call: Call<List<Page>>, response: Response<List<Page>>) {
                    val body = response.body()
                    if (body != null) {
                        callBack.onSuccess(body)
                    } else {
                        callBack.onFailed()
                    }
                }

                override fun onFailure(call: Call<List<Page>>, t: Throwable) {
                    callBack.onFailed()
                }
            })
    }


    fun getFirst(storyId: Int, callBack: ServiceCreator.ICallBack<Page>) {
        pageService.getFirst(storyId)
            .enqueue(object : NetCallBack<Page>() {
                override fun onResponse(call: Call<Page>, response: Response<Page>) {
                    val body = response.body()
                    if (body != null) {
                        callBack.onSuccess(body)
                    } else {
                        callBack.onFailed()
                    }
                }

                override fun onFailure(call: Call<Page>, t: Throwable) {
                    callBack.onFailed()
                }
            })
    }


    fun getById(storyId: Int, callBack: ServiceCreator.ICallBack<Page>) {
        pageService.getById(storyId)
            .enqueue(object : NetCallBack<Page>() {
                override fun onResponse(call: Call<Page>, response: Response<Page>) {
                    val body = response.body()
                    if (body != null) {
                        callBack.onSuccess(body)
                    } else {
                        callBack.onFailed()
                    }
                }

                override fun onFailure(call: Call<Page>, t: Throwable) {
                    callBack.onFailed()
                }
            })
    }

    fun getParentId(storyId: Int, callBack: ServiceCreator.ICallBack<Int>) {
        pageService.getParentId(storyId)
            .enqueue(object : NetCallBack<Int>() {
                override fun onResponse(call: Call<Int>, response: Response<Int>) {
                    val body = response.body()
                    if (body != null) {
                        callBack.onSuccess(body)
                    } else {
                        callBack.onFailed()
                    }
                }

                override fun onFailure(call: Call<Int>, t: Throwable) {
                    callBack.onFailed()
                }
            })
    }

    fun addRecords(records: Records, callBack: ServiceCreator.ICallBack<Boolean>) {
        pageService.addRecords(records)
            .enqueue(object : NetCallBack<Boolean>() {
                override fun onResponse(call: Call<Boolean>, response: Response<Boolean>) {
                    val body = response.body()
                    if (body != null) {
                        callBack.onSuccess(body)
                    } else {
                        callBack.onFailed()
                    }
                }

                override fun onFailure(call: Call<Boolean>, t: Throwable) {
                    callBack.onFailed()
                }
            })
    }

    fun getReadStatic(storyId: Int, callBack: ServiceCreator.ICallBack<List<RecordStatic>>) {
        pageService.getReadStatic(storyId)
            .enqueue(object : NetCallBack<List<RecordStatic>>() {
                override fun onResponse(call: Call<List<RecordStatic>>,
                                        response: Response<List<RecordStatic>>) {
                    val body = response.body()
                    if (body != null) {
                        callBack.onSuccess(body)
                    } else {
                        callBack.onFailed()
                    }
                }

                override fun onFailure(call: Call<List<RecordStatic>>, t: Throwable) {
                    callBack.onFailed()
                }
            })
    }

    fun loadLastReadPage(story: Story, callBack: ServiceCreator.ICallBack<Int>) {
        pageService.loadLastReadPage(story)
            .enqueue(object : NetCallBack<Int>() {
                override fun onResponse(call: Call<Int>,
                                        response: Response<Int>) {
                    val body = response.body()
                    if (body != null) {
                        callBack.onSuccess(body)
                    } else {
                        callBack.onFailed()
                    }
                }
            })
    }

}